import { configByID, gameConfig, gameConfigHandle } from "js/config/gameConfig"
import gameUtils from "js/core/gameUtils"
import { localApi, GameRouteData } from "js/core/localApi"
import utils from "js/core/utils"
import adventureHandle from "js/handle/adventureHandle"
import bagHandle from "js/handle/bagHandle"
import equipmentHandle from "js/handle/equipmentHandle"
import { BagType, BattleResult } from "js/typeDefine/typeDefine"

class AdventureRoute {
    @localApi('adventureRoute')
    battleSettlement(battleResult: BattleResult, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let adventure = player.adventureInfo
        if (battleResult == BattleResult.Win) {
            if (!adventure.mapProgress[adventure.mapID]) {
                adventure.mapProgress[adventure.mapID] = 1
            }
            if (adventure.mapProgress[adventure.mapID] < adventure.floor + 1) {
                adventure.mapProgress[adventure.mapID] = adventure.floor + 1
            }
            adventure.totalFloor += 1
        }
        let res = gameUtils.setMapMax(player, adventure.totalFloor, time)
        if (res) {
            let mapConfig = gameConfigHandle.configByID[adventure.mapID]
            let floorLvl = mapConfig.mapLvl + player.adventureInfo.floor - 2
            if (floorLvl < 1) {
                floorLvl = 1
            }
            if (mapConfig.endless) {
                floorLvl = mapConfig.mapLvl
            }
            let equimentItem = equipmentHandle.create(player, null, floorLvl, random, null)
            if (player.adventureInfo.autoSellSet[equimentItem.equipment.quality]) {
                let getData: any = equipmentHandle.getPriceOfEquipment(equimentItem)
                bagHandle.AddItem(player.bag, getData.itemID, getData.amount, random)
            } else {
                let res = bagHandle.AddItemComponent(player.equipmentBag, equimentItem)
                if (!res) {
                    let getData: any = equipmentHandle.getPriceOfEquipment(equimentItem)
                    bagHandle.AddItem(player.bag, getData.itemID, getData.amount, random)
                }
            }
            let expAmount = adventureHandle.getGoldAndExpAmount(player, time)[1] * 5
            player.teamList.list.forEach(p => {
                if (p) {
                    p.exp += expAmount
                }
            })
            player.itemInfo.gold += expAmount * 2
            return { err: 0, data: expAmount }
        }
        return { err: 0 }
    }

    @localApi('adventureRoute')
    battleLost(mapID, floor, gameRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gameRouteData
        if (player.floorStop.mapID == mapID && player.floorStop.floor == floor) {
            player.floorStop.stopTimes = (player.floorStop.stopTimes || 0) + 1
        } else {
            let triggerTimes = player.floorStop.triggerTimes || 0
            let stopTimes = player.floorStop.stopTimes || 0
            if (stopTimes >= gameConfigHandle.gameConfig.map.floorStopMaxTrigger) {
                player.floorStop.triggerTimes = triggerTimes + 1
            }
            player.floorStop.mapID = mapID
            player.floorStop.floor = floor
            player.floorStop.stopTimes = 1
            player.floorStop.triggerTimes = player.floorStop.triggerTimes || 0
        }
        return {
            err: 0
        }
    }

    @localApi('adventureRoute')
    nextFloor(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        if (player.baseInfo.disableAutoMap) {
            return { err: 100 }
        }
        let adventure = player.adventureInfo
        let mapConfig = gameConfigHandle.configByID[adventure.mapID]
        let progress = adventure.mapProgress[adventure.mapID] || 0

        if (progress <= adventure.floor) {
            return { err: 100 }
        }
        if (adventure.floor >= 10) {
            if (mapConfig.nextMapID) {
                adventure.mapID = mapConfig.nextMapID
                adventure.floor = 1
            } else {
                adventure.floor += 1
            }
        } else {
            adventure.floor += 1
        }
        return { err: 0 }
    }

    @localApi('adventureRoute')
    nextNewFloor(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        if (player.baseInfo.disableAutoMap) {
            return { err: 100 }
        }
        let adventure = player.adventureInfo
        if (adventure.newFloor >= 10) {
            adventure.newFloor = 10
        } else {
            adventure.newFloor += 1
        }
        let progress = adventure.newMapProgress[adventure.newMapID] || 1
        if (progress < adventure.newFloor) {
            adventure.newMapProgress[adventure.newMapID] = adventure.newFloor
        }
        return { err: 0 }
    }

    @localApi('adventureRoute')
    newBattleSettlement(battleResult: BattleResult, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let adventure = player.adventureInfo
        let mapConfig = gameConfigHandle.configByID[adventure.newMapID]
        let equimentItem = equipmentHandle.createForFantasy(player, null, mapConfig.mapLvl, random, null)
        // 仅第一次通关掉落
        let dropEq = false
        if(adventure.newMapProgress[adventure.newMapID] < adventure.newFloor) { 
            dropEq = true
            if (player.adventureInfo.autoSellSet[BagType.Equipment2] && player.adventureInfo.autoSellSet[BagType.Equipment2][equimentItem.equipment.quality - 1]) {
                let getData: any = equipmentHandle.getPriceOfEquipment(equimentItem)
                bagHandle.AddItem(player.bag, getData.itemID, getData.amount, random)
            } else {
                let res = bagHandle.AddItemComponent(player.equipmentBag2, equimentItem)
                if (!res) {
                    let getData: any = equipmentHandle.getPriceOfEquipment(equimentItem)
                    bagHandle.AddItem(player.bag, getData.itemID, getData.amount, random)
                }
            }
        }
        
        let dropItems = mapConfig.dropItems
        let dropItem: any = utils.getOneFromArray(dropItems, random)
        let drops = []
        if (dropItem && random.intn(100) < 10) {
            bagHandle.AddItem(player.bag, dropItem, 1, random)
            drops.push([dropItem, 1])
        }
        if (random.intn(100) < (mapConfig.rareDropRate || gameConfig.newland.rareDropRate)) {
            dropItems = mapConfig.rareDropItems
            dropItem = utils.getOneFromArray(dropItems, random)
            if (dropItem) {
                bagHandle.AddItem(player.bag, dropItem, 1, random)
            }
            drops.push([dropItem, 1])
        }
        let get = adventureHandle.getGoldAndExpAmount(player, time, true)
        player.teamList.list.forEach(p => {
            if (p) {
                p.exp += get[1]
            }
        })
        player.itemInfo.diamond2 = (player.itemInfo.diamond2 || 0) + get[2]
        return { err: 0, data: { exp: get[1], diamond2: get[2], dropItems: drops, dropEq } }
    }
}

export let adventureRoute: AdventureRoute = new AdventureRoute()